<?php

/**
 * @author      :   HungNT1
 * @name        :   VideoController
 * @copyright   :   Fpt Online
 * @todo        :   Video Controller
 */
class Euro2012_VideoController extends Zend_Controller_Action
{

    /**
     * init of controller
     */
    public function init()
    {
        //Disable layout
        //$this->_helper->layout->setLayout('common');

    }

    //index action
    public function indexAction()
    {        
        //get category id
        $cate_id = $this->_request->getParam('cateid');

        //get video id
        $video_id = $this->_request->getParam('videoid');

        //get title video
        $title = $this->_request->getParam('title');

        //init model video
        $newsModel = News::getInstance();

        //----------------------------------------------------------------------
        //get 1 video and 6 video host at the top and box top view of the website

        //limit video hot
        $intLimitHotVideo = LIMIT_HOT_VIDEO_SITE;

        //init param for get list video top
        $arr_param = array(
                        'article_type' => VIDEO,
                        'category_id' => CATE_ID_VIDEO, //$cateid( co data truyen $cateid)
                        'limit' => $intLimitHotVideo,
                        'offset' => 0,
                        'site_id' => SITE_ID
        );

        
        //get hot video with not excluse
        $arrResult = $newsModel->getListArticleByHotNotExcluse($arr_param);
        
        $arrVideoHot = $arrResult['data'];
        
        //check data, if video top < limit then merge with top view video
        if ($arrResult['total'] < $intLimitHotVideo && !empty ($arrResult['total']) )
        {
            $statictisModel = Statictis::getInstance();

            //get video top view
            $arrTopView = $statictisModel->getTopViewArticle(VIDEO, CATE_ID_VIDEO, $intLimitHotVideo);

            if (!empty($arrTopView)) {
                $arrVideoHot = array_merge($arrTopView, $arrVideoHot);
                $arrVideoHot = array_slice(array_unique($arrVideoHot), 0, $intLimitHotVideo);
            }

        }

        //get video detail with list video id
        $videoDetail = array();
        $arrVideoHotDetail = array();
        //check videoid, if empty then get with array video id
        if (empty ($video_id))
        {
            if (!empty ($arrVideoHot))
            {
                $arrVideoHotDetail = $newsModel->getDetailArticleByIDs($arrVideoHot);
        
                //get 1 video detail in top page
                $videoDetail = current($arrVideoHotDetail);
                unset ($arrVideoHotDetail[$videoDetail['article_id']]);

            }
        }
        else {
            //get video detail with video id
            $videoDetail = $newsModel->getDetailArticleByIDs(array($video_id));
            $videoDetail = $videoDetail[$video_id];
            //remove video in video hot
            $arrVideoHot = array_diff($arrVideoHot, array($video_id));

            if (!empty ($arrVideoHot))
            {
                $arrVideoHotDetail = $newsModel->getDetailArticleByIDs($arrVideoHot);
            }
        }
        
        //assign to view
        $this->view->videoDetail = $videoDetail;
        $this->view->arrVideoHot = $arrVideoHotDetail;

        //get cate id
        $cate_id = ( $video_id > 0 && !empty($videoDetail)) ? $videoDetail["category_id"] : CATE_ID_VIDEO;

        //----------------------------------------------------------------------
        //get 2 video at the top and 15 video newest at the box newest
        if (!empty($videoDetail))
        {
            //get list tag by article
            $arrTag = $newsModel->getTagsByArticleID($videoDetail["article_id"]);
            $arrMediaResult = array();
            if (!empty($arrTag))
            {
                $totalTag = count($arrTag);
                for ($i = 0; $i < $totalTag; $i++)
                {
                    //get list article by tag
                    $arrMedia = $newsModel->getListArticleByTag($arrTag[$i], LIMIT_RELATED_MEDIA, 0, VIDEO);
                    
                    if ($arrMedia["total"] > 0)
                    {
                        $arrMediaResult = array_merge($arrMediaResult, $arrMedia["data"]);
                        
                        //excluse current video play
                        $arrMediaResult = array_diff(array_unique($arrMediaResult), $arrVideoHot);
                        
                    }
                    if ($arrMedia["total"] <= 0 && count($arrMediaResult) < LIMIT_RELATED_MEDIA)
                    {
                        continue;
                    }
                    else
                    {
                        break;
                    }
                }
            }
            //toltal media
            $totalMedia = count($arrMediaResult);
            //if total media < 2 then get new video
            if ($totalMedia < LIMIT_RELATED_MEDIA)
            {
                $limit_more  = (LIMIT_RELATED_MEDIA - $totalMedia) + $limitTopHot; //( $limitTopHot là total top & hot video)
                //get list articleid new
                $arrNewVideo = $newsModel->getListTopArticle($cate_id, $limit_more, 0, VIDEO);
                if (!empty($arrNewVideo["data"]))
                {
                    //merge media & new video
                    $arrMediaResult = array_merge($arrMediaResult, $arrNewVideo["data"]);
                }
            }
            //get article detail list
            if (!empty($arrMediaResult))
            {
                //excluse video top+hot
                $arrMediaResult   = array_diff(array_unique($arrMediaResult), $arrVideoHot);
                $arrMediaResult   = array_splice($arrMediaResult, 0, LIMIT_RELATED_MEDIA);
                // merge excluse to get new video list
                $arrVideoHot = array_merge($arrVideoHot, $arrMediaResult);
                // ge detail
                $arrMediaResult   = $newsModel->getDetailArticleByIDs($arrMediaResult);
            }
        }
        //assign data
        $this->view->arrMediaResult = $arrMediaResult;
        
        //----------------------------------------------------------------------
        //get 2 video at the top and 15 video newest at the box newest
        //get cate id
        $cate_id = ( $video_id > 0 && !empty($videoDetail)) ? $videoDetail["category_id"] : CATE_ID_VIDEO;

        //get param number page
        $intPage = $this->_request->getParam('page', 1);

        //Set start offset
        $perpage = LIMIT_NEW_VIDEO_SITE + $intLimitHotVideo;
        $intStart = ($intPage - 1) * $perpage;

        //get list newest video
        $arrNewVideoResult = $newsModel->getListArticleByCate($cate_id, $perpage, $intStart, VIDEO);
        
        if (!empty ($arrNewVideoResult['data']) && !empty ($arrVideoHot))
        {
            //slice and diff video id
            $arrNewVideoResult['data'] = array_slice(array_diff($arrNewVideoResult['data'], $arrVideoHot), 0, LIMIT_NEW_VIDEO_SITE);
        }
        // Get total video
        $totalVideo   = count($arrNewVideoResult['data']);
        
        //get detail video by list id
        $arrNewestVideoDetail = array();
        if (!empty($arrNewVideoResult) && !empty($arrNewVideoResult["data"])) {
            $arrNewestVideoDetail = $newsModel->getDetailArticleByIDs($arrNewVideoResult["data"]);
        }

        $this->view->arrVideoData = $arrNewestVideoDetail;

        //----------------------------------------------------------------------
        //get cate with euro
        $cate_video_euro = 1002621;

        $cateModel = Category::getInstance();
        $menu = $cateModel->getMenu(CATE_EURO_ID);


        $this->view->arrMenuVideoEuro = $menu['child'][$cate_video_euro];

        $cateDetail = $cateModel->getDetailbyCateId($cate_id);

        $intTotal = $totalVideo;
        $strUri = '/video-euro/'.$cateDetail['catecode'].'/'.$title.'-'.$video_id;


        //InitParam Paging
        $arrParamPaging = array(
            'total'         =>  $intTotal,
            'page'          =>  $intPage,
            'uri'           =>  $strUri,
            'showItem'      =>  3,
            'perpage'       =>  $perpage,
            'idPagination'  =>  'pagination',
            'extEnd'        =>  '.html',
            'separate'      =>  '-'
        );
        
        $this->view->totalVideo = $totalVideo;
        $this->view->arrParamPaging = $arrParamPaging;


               
    }

     /*
     * Get data video for xml
     */

    function vneInfoAction()
    {
        $this->_helper->layout()->disableLayout();
        $intVideoID = (int) $this->getRequest()->getParam('id', 0);

        if ($intVideoID > 0 && $intVideoID < 5000000)
        {
            // Get detail article
            $newsModel  = News::getInstance();
            $arrArticle = $newsModel->getDetailArticleByIDs(array($intVideoID));
            $arrMedia = array();
            if (!empty($arrArticle))
            {
                // Reference article
                $arrMedia   = $newsModel->getObjectReferenceByArticleID($intVideoID, 1, 0);
                $arrArticle = $arrArticle[$intVideoID];
                if (!empty($arrMedia))
                {
                    $arrMedia = current($arrMedia);
                }
            }
            $this->view->arrMedia = $arrMedia;
            $this->view->arrArticle = $arrArticle;
        }
        $this->view->videoID = $intVideoID;
    }

    function vneMoreAction()
    {
        $this->_helper->layout()->disableLayout();
    }

    public function loadListVideoAction()
    {
        // Disable render layout
        $this->_helper->layout->disableLayout(true);
        $this->_helper->viewRenderer->setNoRender(true);

        // Default response
        $response = array(
            'error' => 0,
            'msg'   => 'Success',
            'html'  => NULL,
            'ext'   => NULL
        );

        // Get params
        $arrVideoIds = $this->_request->getParam('listVideoIds', NULL);
        $cateID      = intval($this->_request->getParam('cateID', 0));
        $cateName    = $this->_request->getParam('cateName', NULL);
        $currPage    = intval($this->_request->getParam('currPage', 0));
        $totalPage   = intval($this->_request->getParam('totalPage', 0));
        $totalVideo  = intval($this->_request->getParam('totalVideo', 0));
        $arrExcluse = $this->_request->getParam('excluse', NULL);

        // Get model's instance
        $newsModel = News::getInstance();

        // if hasn't cate => load by id
        if (!$cateID)
        {
            if (is_array($arrVideoIds) && count($arrVideoIds) && !empty($cateName) && $totalPage && $totalVideo && $currPage)
            {
                // Assign to view
                $this->view->assign(array(
                    'cateName'     => $cateName,
                    'totalPage'    => $totalPage,
                    'totalVideo'   => $totalVideo,
                    'currPage'     => $currPage,
                    'arrVideoData' => $newsModel->getDetailArticleByIDs($arrVideoIds),
                ));

                $response['html'] = $this->view->render('video/box/box_list_video.phtml');
            }
            else
            {
                $response['error'] = 1;
                $response['msg']   = 'Invalid parameters';
            }
        }
        else
        {
            $cateModel = Category::getInstance();

            // Get cate's detail
            $cateDetail = $cateModel->getDetailbyCateId($cateID);

            //get list article id by category's id
            $arrNewVideoResult = $newsModel->getListTopArticle($cateID, 0, 0, VIDEO);

            // Get true data
            $arrNewVideoResult['data'] = array_diff($arrNewVideoResult['data'], $arrExcluse);

            // Get total video
            $totalVideo   = count($arrNewVideoResult['data']);
            $arrVideoData = array();
            if ($totalVideo)
            {
                $arrVideoIds = array_slice($arrNewVideoResult['data'], 0, LIMIT_NEW_VIDEO_SITE);

                // Get video detail by ids
                $arrVideoData = $newsModel->getDetailArticleByIDs($arrVideoIds);
            }

            $totalPage = $totalVideo ? intval(ceil($totalVideo / LIMIT_NEW_VIDEO_SITE)) : 0;

            // Assign to view
            $this->view->assign(array(
                'cateName'     => strtolower($cateDetail['catename']) == 'video' ? 'Thể thao' : $cateDetail['catename'],
                'totalVideo'   => $totalVideo,
                'totalPage'    => $totalPage,
                'currPage'     => 1,
                'arrVideoData' => $arrVideoData,
            ));

            $response['html'] = $this->view->render('video/box/box_list_video.phtml');
            $response['ext']  = array(
                'currPage'     => 1,
                'totalVideo'   => $totalVideo,
                'totalPage'    => $totalPage,
                'listVideoIds' => $totalVideo ? array_chunk($arrNewVideoResult['data'], LIMIT_NEW_VIDEO_SITE) : array(),
            );
        }

        echo Zend_Json::encode($response);

        exit;
    }
}